<?php
// +----------------------------------------------------------------------
// | QSCMS
// +----------------------------------------------------------------------
// | Copyright (c)  2025 https://www.qqss.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed QSCMS is not free software, commercial use must purchase official license from official website to avoid unnecessary legal disputes.
// +----------------------------------------------------------------------
// | Author: Contract 990504246@qq.com
// +----------------------------------------------------------------------

namespace app\api\controller\locoy;

use app\api\controller\Base;
use app\common\model\Goods;
use app\common\model\GoodsContentChapter;

class Index extends Base
{

    /**
     * 根据商品名查找最后一个章节
     * @param string $goodsName 商品名称
     * @return array|null
     */
    public function findLastChapterByGoodsName()
    {
        // 查找商品
        $name  = inputs('name');
        $goods = Goods::where('name', $name)->find();
        if (!$goods) {
            $this->error(trans('goods_not_exists'));
        }

        // 查找最后一个章节
        $chapter = GoodsContentChapter::getLastChapter($goods->id);
        if (!$chapter) {
            $this->error('章节不存在');
        }

        $goodsName = $goods->getData('name');

        return $this->success('获取成功', [
            'id'         => $chapter->id,
            'title'      => $chapter->title,
            'goods_id'   => $chapter->goods_id,
            'goods_name' => $goodsName
        ]);
    }
}